<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Cache</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Cache</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
    <p>The Cache Utility provides a basic caching mechanism for storing key/value pairs in local JavaScript memory. As a subclass of Plugin, it is designed to seamlessly integrate with other components (such as <a href="../datasource/" title="YUI 3: DataSource">DataSource</a>).</p>
</div>

<h2 id="upgrade">Upgrade Notes</h2>

<dl>
    <dt>Cache as a plugin has changed</dt>
    <dd>Implementers upgrading from version 3.1.1 or earlier who are using Cache as a plugin now need to point to Y.Plugin.Cache instead of Y.Cache.</dd>
</dl>

<h2 id="getting-started">Getting Started</h2>

<p>
To include the source files for Cache and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>

<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.4.1&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>


<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
YUI().use(&#x27;cache&#x27;, function (Y) {
    &#x2F;&#x2F; Cache is available and ready for use. Add implementation
    &#x2F;&#x2F; code here.
});</pre>


<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global object</a>.
</p>


<h2 id="using">Using the Cache</h2>

<p>This section describes how to use the Cache in further detail.</p>

<h3 id="base">Basic caching</h3>

<p>Basic caching allows you to store frequently used data in local JavaScript memory. In cases where data is retrieved over the wire from a server, you can store the response in a local cache and eliminate future trips to the server for better performance and reduced server load.</p>

<p>Use the following Attributes to configure your Cache instance:</p>

<table>
<thead>
    <tr>
        <th>Attribute</th>
        <th>Default</th>
        <th>Description</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><code>max</code></td>
        <td><code>0</code></td>
        <td>The maximum number of entries the cache can hold. <strong>The cache is disabled by default!</strong> Set this value to a number greater than zero to turn on caching.</td>
    </tr>
    <tr>
        <td><code>size</code></td>
        <td>n/a</td>
        <td>Read-only. Returns the number of entries currently stored in the cache.</td>
    </tr>
    <tr>
        <td><code>entries</code></td>
        <td>n/a</td>
        <td>Read-only. Returns an array of the entries currently stored in the cache.</td>
    </tr>
    <tr>
        <td><code>expires</code></td>
        <td><code>0</code></td>
        <td>By default, expiration is disabled. To enable expiration of data, set this value to the Date when the data should expire, or the relative number of milliseconds from collection that the data should expire.</td>
    </tr>
    <tr>
        <td><code>uniqueKeys</code></td>
        <td><code>false</code></td>
        <td>When calling add() with an entry, checks to see if the key is already stored in the cache. Enforcing unique keys requires iterating through all stored entries, so setting this attribute to <code>false</code> is more performant. Note: If expiration is enabled, you should probably set uniqueKeys to <code>false</code> to avoid problems if data is cached multiple times with conflicting expirations.</td>
    </tr>
</tbody>
</table>

<p>Here are the ways you can set the <code>max</code> value</strong> of a cache. <strong>You must set a <code>max</code> value to turn on caching.</strong></p>
        
<pre class="code prettyprint">&#x2F;&#x2F; Configure Cache maximum size in the constructor
var myCache = new Y.Cache({max:5});

&#x2F;&#x2F; Set the maximum size at runtime
myCache.set(&quot;max&quot;, 10);</pre>


<p><strong>Cache key/value pairs with the <code>add()</code> method.</strong></p>

<pre class="code prettyprint">&#x2F;&#x2F; Add entries to the Cache
myCache.add(&quot;key1&quot;, &quot;value1&quot;);</pre>

<p><strong>Retrieve cached entries with the <code>retrieve()</code> method.</strong> If there is no match for the given key, then <code>null</code> will be returned. Cached entries contain the properties <code>request</code>, <code>response</code>, <code>cached</code>, and <code>expires</code>.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Retrieve a cached entry
var cachedEntry = cache.retrieve(&quot;key1&quot;);</pre>


<p>By default, cached entries may contain duplicate keys: if you add an entry for request "foo" with value "bar" and then add another entry for request "foo" with value "bat", the cache will contain both entries. Retrieving an entry for request "foo" will only retrieve the newest value. However, setting <code>uniqueKeys</code> to <code>true</code> will validate for the uniqueness of keys stored in the cache.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Enforce unique keys in the constructor
var myCache = new Y.Cache({max:5, uniqueKeys:true});

&#x2F;&#x2F; Enforce unique keys at runtime
myCache.set(&quot;uniqueKeys&quot;, true);</pre>


<p>A cache may be flushed of all its entries with the <code>flush()</code> method.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Flush the cache
myCache.flush();</pre>

<h3 id="offline">Offline caching</h3>

<p>The CacheOffline cache extends basic caching functionality by storing data in the <a href="http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage" title="Storing Objects in HTML5 localStorage - Stack Overflow">HTML5 localStorage object</a> on browsers where that is supported, so that data can be available when the browser is offline and can persist across browser sessions. In cases where HTML5 localStorage is not available (i.e., IE 6 and IE 7), the functionality falls back to basic caching in local JavaScript memory.</p>

<pre class="code prettyprint">var myCache = new Y.CacheOffline();</pre>


<p>Use the following Attributes to configure your CacheOffline instance:</p>

<table>
<thead>
    <tr>
        <th>Attribute</th>
        <th>Default</th>
        <th>Description</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><code>sandbox</code></td>
        <td><code>&quot;default&quot;</code></td>
        <td>A unique identifier used to sandbox each cache instance's entries from other entries. This string persists across browser sessions so take care that it is not dynamically generated.</td>
    </tr>
    <tr>
        <td><code>expires</code></td>
        <td><code>86400000</code></td>
        <td>By default, data will expire one day after it is cached. This Attribute accepts a Data value, or a number of milliseconds.</td>
    </tr>
    <tr>
        <td><code>max</code></td>
        <td><code>null</code></td>
        <td>Read-only. This Attribute is disabled for CacheOffline &#8212; there is no notion of capping the number of entries in a cache. Each browser implements a maximum localStorage byte size.</td>
    </tr>
    <tr>
        <td><code>uniqueKeys</code></td>
        <td><code>true</code></td>
        <td>Read-only. This Attribute is disabled for CacheOffline. All stored keys are unique.</td>
    </tr>
</tbody>
</table>

<h3 id="plugin">Cache as a plugin</h3>

<p>Use the <code>plug(Y.Plugin.Cache)</code> method on a host object to enable caching. The Y.Plugin.Cache class is available in the "cache-plugin" submodule.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Define a max value to enable plugging.
myWidget.plug(Y.Plugin.Cache, {max:3});
myWidget.cache.add(&quot;key&quot;, &quot;value&quot;);</pre>


<p>The Cache plugin also accepts a <code>cache</code> value to enable you to point to any number of Cache implementations. Y.Cache and Y.CacheOffline are currently available.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Define a max value to enable plugging.
myWidget.plug(Y.Plugin.Cache, {cache:Y.CacheOffline});</pre>


<p>The Y.Plugin.DataSourceCache plugin enables seamless caching of DataSource responses.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Use a basic cache
myDataSource.plug(Y.Plugin.DataSourceCache, {
cache: Y.Cache, &#x2F;&#x2F; this is the default, this line is not needed
max: 100
});

&#x2F;&#x2F; Use the &quot;cache&quot; configuration property to enable offline caching
myDataSource.plug(Y.Plugin.DataSourceCache, {
cache: Y.CacheOffline,
sandbox: &quot;my3HrCache&quot;,
expires: 10800000 &#x2F;&#x2F; 3 hours
});</pre>


<p>Once the DataSourceCache plugin is enabled, it handles caching and retrieval of values without the need for extra code. Furthermore, all the methods and properties of the Cache instance is available on the host's <code>cache</code> namespace.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Flush myDataSource&#x27;s cache.
myDataSource.cache.flush();</pre>


<h3 id="events">Events</h3>

<table>
<thead>
    <tr>
        <th>Event</th>
        <th>When</th>
        <th class="wrap">Properties available on the Event facade passed to handler</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><code>add</code></td>
        <td>Entry is added to the cache.</td>
        <td><dl>
            <dt><code>entry</code></dt>
            <dd>The cached entry.</dd>
        </dl></td>
    </tr>
    <tr>
        <td><code>request</code></td>
        <td>Entry is requested from the cache.</td>
        <td><dl>
            <dt><code>request</code></dt>
            <dd>The request value.</dd>
        </dl></td>
    </tr>
    <tr>
        <td><code>retrieve</code></td>
        <td>Entry is retrieved from the cache.</td>
        <td><dl>
            <dt><code>entry</code></dt>
            <dd>The retrieved entry.</dd>
        </dl></td>
    </tr>
    <tr>
        <td><code>flush</code></td>
        <td>Cache is flushed.</td>
        <td>none</td>
    </tr>
    <tr>
        <td><code>error</code></td>
        <td>CacheOffline only. Fired when an entry could not be added, most likely due to exceeding the browser quota for the localStorage object.</td>
        <td><dl>
            <dt><code>error</code></dt>
            <dd>The error object.</dd>
        </dl></td>
    </tr>
</tbody>
</table>
</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#upgrade">Upgrade Notes</a>
</li>
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using">Using the Cache</a>
<ul class="toc">
<li>
<a href="#base">Basic caching</a>
</li>
<li>
<a href="#offline">Offline caching</a>
</li>
<li>
<a href="#plugin">Cache as a plugin</a>
</li>
<li>
<a href="#events">Events</a>
</li>
</ul>
</li>
</ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Basic caching functionality with the Cache Utility.">
                                        <a href="cache-basic.html">Basic Caching</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Offline caching implements HTML 5 localStorage when available, to allow data to persist across browser sessions.">
                                        <a href="cache-offline.html">Offline Caching</a>
                                    </li>
                                
                            
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                                    <li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
                                        <a href="../datasource/datasource-caching.html">DataSource with Caching</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
                                        <a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
